/*
* Copyright 2008 NOR_/D <http://nortd.com>
*
* This file is part of ofxTouch.
* ofxTouch is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as 
* published by the Free Software Foundation, either version 3 of 
* the License, or (at your option) any later version. For details
* see <http://www.gnu.org/licenses/>.
*
* * *
*/


#ifndef _OFX_TOUCH_EVENTS
#define _OFX_TOUCH_EVENTS

#include "ofEvents.h"
#include "ofxTouchTrackedBlob.h";


// part of arg in touch events
class ofxTouch {
  public:
	
    int id;
    int initialOrder;
    int x;
    int y;
    int width;
    int height;
    float radius;
    
	
    ofxTouch() {
        id = -1;
        radius = 0.0f;
    }
    
    ofxTouch( const ofxTouchBlob& b ) {
        id = -1;
        initialOrder = -1;       
        x = b.center.x;
        y = b.center.y;
        width = b.box.width;
        height = b.box.height;
        if( b.box.width > b.box.height ) { radius = b.box.width/2.f; }
        else { radius = b.box.height/2.f; }
    }
	
    ofxTouch( const ofxTouchTrackedBlob& b ) {
        id = b.id;
        initialOrder = -1;
        set(b);
    }    
	
    void set( const ofxTouchTrackedBlob& b, float scale=1.0f ) {
        x = b.center.x*scale;
        y = b.center.y*scale;
        width = b.box.width*scale;
        height = b.box.height*scale;
        if( b.box.width > b.box.height ) { radius = (b.box.width/2.f)*scale; }
        else { radius = (b.box.height/2.f)*scale; }        
    }
};



class ofxTouches {
  public:
    
    //double timestamp;
    
    virtual ofxTouch& operator[]( const int& i ) {
        return *(touchesForCurrentPhase[i]);
    }
    
    virtual size_t size() { return touchesForCurrentPhase.size(); }
    
    /*
    virtual vector<ofxTouch*> getAlltouches() {}
    virtual vector<ofxTouch*> getTouchesForPhase() {}
    virtual vector<ofxTouch*> getTouchesForPhase( int phase ) {}
    virtual vector<ofxTouch*> getTouchesForView() {}
    virtual vector<ofxTouch*> getTouchesForWindow() {}
    */
    
    virtual void addTouch( ofxTouch* touch) {
        touchesForCurrentPhase.push_back( touch );
    }    
    
  protected:
    int currentPhase;
    vector<ofxTouch*> touchesForCurrentPhase;
    //vector<ofxTouch*> allTouches;
   
};


class ofxTouchEventsClass {
  public:

    ofEvent<ofxTouchTrackedBlob>  blobDown;
    ofEvent<ofxTouchTrackedBlob>  blobMove;
    ofEvent<ofxTouchTrackedBlob>  blobUp;

    ofEvent<ofxTouch>  down;
    ofEvent<ofxTouch>  move;
    ofEvent<ofxTouch>  up;
    ofEvent<ofxTouch>  cancel;    
	
    void disable(){
        blobDown.disable();
        blobMove.disable();
        blobUp.disable();
        down.disable();
        move.disable();
        up.disable();        
        cancel.disable();        
    }

    void enable(){
        blobDown.enable();
        blobMove.enable();
        blobUp.enable();    
        down.enable();
        move.enable();
        up.enable();
        cancel.enable();
    }
};


// pseudo singleton
extern ofxTouchEventsClass ofxTouchEvents;


#endif

